package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallIntegralLogMapper;
import org.linlinjava.litemall.db.domain.LitemallIntegralLog;
import org.linlinjava.litemall.db.domain.LitemallIntegralLogExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

 

/**
 * @Author: stephen
 * @Date: 2020/2/13 13:37
 * @Version: 1.0
 * @Description: TODO
 */
@Service
public class LitemallIntegralLogService extends MPJBaseServiceImpl<LitemallIntegralLogMapper,LitemallIntegralLog> {
    @Resource
    private LitemallIntegralLogMapper litemallIntegralLogMapper;

    public List<LitemallIntegralLog> querySelectiveList(Integer userId, Byte type, Integer page, Integer size, String sort, String order) {
        LitemallIntegralLogExample example = new LitemallIntegralLogExample();
        LitemallIntegralLogExample.Criteria criteria = example.createCriteria();

        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(type != null){
        	criteria.andTypeEqualTo(type);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

      if(page != null && size != null){
          //PageHelper.startPage(page, size);
      }

        return litemallIntegralLogMapper.selectByExample(example);
    }

    public List<LitemallIntegralLog> queryByUserId(Integer userId,Integer page,Integer limit,String sort,String order,Byte type){
        LitemallIntegralLogExample example = new LitemallIntegralLogExample();

        if(type != null){
            example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false).andTypeEqualTo(type);
        }else{
            example.or().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        }


        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        //PageHelper.startPage(page, limit);
        return litemallIntegralLogMapper.selectByExample(example);
    }


    public int add(LitemallIntegralLog record){
        record.setAddTime(LocalDateTime.now());
        return litemallIntegralLogMapper.insertSelective(record);
    }

    public LitemallIntegralLog queryByConsumptionIdOrOrderId(Integer consumptionId,Integer orderId){
        LitemallIntegralLogExample example = new LitemallIntegralLogExample();
        LitemallIntegralLogExample.Criteria criteria = example.createCriteria();
        if(consumptionId != null){
            criteria.andConsumptionIdEqualTo(consumptionId);
        }

        if(orderId != null){
            criteria.andOrderIdEqualTo(orderId);
        }

        return litemallIntegralLogMapper.selectOneByExample(example);
    }



}
